﻿//implemented by moses
//implemented on 12/01/22

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TravelAgentsDesktopDBApp
{
    public partial class FormProducts : Form
    {
        //class vars
        private Products _selectedProd = null;


        public FormProducts()
        {
            InitializeComponent();
        }

        private void showList()
        {
            //function to show the list pane and hide the inputs

            gpbProductsInfo.Visible = false;
            gpbProductsInfo.Enabled = false;
            btnProdBack.Visible = false;
            btnProdBack.Enabled = false;
            gpbProductsList.Visible = true;
            gpbProductsList.Enabled = true;
        }

        private void showInput()
        {
            //function to show the inputs and hide the list pane

            gpbProductsInfo.Visible = true;
            gpbProductsInfo.Enabled = true;
            btnProdBack.Visible = true;
            btnProdBack.Enabled = true;
            gpbProductsList.Visible = false;
            gpbProductsList.Enabled = false;
        }

        private void populateLstProd()
        {
            //function to populate lstProducts with products which match the search terms

            lstProducts.Items.Clear();
            lstProducts.Items.Add("CREATE NEW PRODUCT");
            lstProducts.Items.Add("");

            List<Products> products = new Search().filterProdName(this);
            
            foreach (Products product in products)
                { lstProducts.Items.Add(product); }

        }

        private void populateLstProd(Products entry)
        {
            //function to populate lstProducts with selected product details
            //and some commands

            lstProducts.Items.Clear();

            lstProducts.Items.Add("ID: " + entry.ProductId);
            lstProducts.Items.Add("NAME: " + entry.ProdName);
            lstProducts.Items.Add("");
            lstProducts.Items.Add("EDIT");
            lstProducts.Items.Add("DELETE");
            lstProducts.Items.Add("BACK");

        }

        private void populateProdDeets(Products entry)
        {
            txtProductId.Enabled = false;
            txtProductId.Text = entry.ProductId.ToString();
            txtProductName.Text = entry.ProdName;
        }

        private void resetInputs()
        {
            //clear prod name
            txtProductName.Text = "";

            //set prod id to "AUTO-GENERATED"
            txtProductId.Enabled = false;
            txtProductId.Text = "AUTO-GENERATED";

            //clear the search field as well
            txtSearchProducts.Text = "";

            //clear selected
            _selectedProd = null;

        }

        private void exitProductForm_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FormProducts_Load(object sender, EventArgs e)
        {
            populateLstProd();
            showList();
            txtProductId.Enabled = false;
        }

        private void txtSearchProducts_TextChanged(object sender, EventArgs e)
        {
            if (_selectedProd == null) { populateLstProd(); }
        }

        private void btnProdRes_Click(object sender, EventArgs e)
        {
            if (_selectedProd != null) { populateProdDeets(_selectedProd); }
            else { resetInputs(); }
        }

        private void btnBack_Click(object sender, EventArgs e)
        {
            if (_selectedProd == null)
            {
                resetInputs();
                populateLstProd();
            }

            else { populateLstProd(_selectedProd); }

            showList();
        }

        private void lstProducts_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lstProducts.SelectedItem != null)
            {
                if (lstProducts.SelectedItem.GetType() == typeof(Products))
                {
                    _selectedProd = (Products)lstProducts.SelectedItem;
                    populateLstProd(_selectedProd);
                }
                else
                {
                    switch (lstProducts.SelectedItem.ToString())
                    {
                        case "CREATE NEW PRODUCT":
                            btnProdMulti.Text = "SAVE";
                            btnProdBack.Text = "BACK TO LISTING";
                            resetInputs();
                            showInput();
                            break;

                        case "EDIT":
                            btnProdMulti.Text = "UPDATE";
                            btnProdBack.Text = "BACK TO SELECTED PRODUCT";
                            populateProdDeets(_selectedProd);
                            showInput();
                            break;

                        case "BACK":
                            resetInputs();
                            populateLstProd();
                            showList();
                            break;

                        case "DELETE":
                            //ask user for delete confirmation, otherwise do nothing

                            if (_selectedProd != null)
                            {

                                //show a msg box to confirm deletion
                                DialogResult confirm = MessageBox.Show("Do you really want to delete " + _selectedProd.ProdName + "?",
                                    "Confirm Product Deletion", MessageBoxButtons.YesNo);

                                if (confirm == DialogResult.Yes)
                                {
                                    new DbAccess().DeleteProducts(_selectedProd);
                                    resetInputs();
                                    populateLstProd();
                                    showList();
                                }
                            }

                            else
                            { MessageBox.Show("Could not delete!\nUnknown error!"); }

                            break;

                        default: break;
                    }
                }
            }
        }

        private Products getProduct()
        {
            //function validates user input as it tries to make a new package
            bool isValid = true;
            Products entry = new Products();


            foreach(Control ctrl in gpbProductsInfo.Controls)
            {
                switch (ctrl.Name)
                {
                    case "txtProductName":
                        if (txtProductName.Text != "")
                            { entry.ProdName = txtProductName.Text; }
                        else 
                        {
                            isValid = false;
                            MessageBox.Show("Product name is required");
                        }

                        break;

                    case "txtProductId":
                        int id;
                        if (int.TryParse(txtProductId.Text, out id))
                            { entry.ProductId = id; }
                        break;

                    default: break;

                }
            }

            if (!isValid) { return null; }
            else { return entry; }

        }

        private void btnProdMulti_Click(object sender, EventArgs e)
        {
            switch (btnProdMulti.Text)
            {
                case "SAVE":

                    Products newCandi = getProduct();
                    if (newCandi != null) { new DbAccess().InsertProducts(newCandi); }
                    resetInputs();
                    populateLstProd();
                    showList();
                    break;

                case "UPDATE":

                    Products updCandi = getProduct();
                    if (updCandi != null) { new DbAccess().UpdateProducts(updCandi); }
                    resetInputs();
                    populateLstProd();
                    showList();
                    break;

                default: break;
                    

            }
        }



    }
}
